package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.LawyerDaoImpl;
import com.wanxi.dao.LawyerDao;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.LawyerModel;
import com.wanxi.service.LawyerService;

/**
 * @author Li
 * @Date 2023/5/6 14:30
 */
public class LawyerServiceImpl implements LawyerService {
    private LawyerDao lawyerDao = new LawyerDaoImpl();

    /**
     * 新增数据
     *
     * @param lawyerModel
     * @return
     */
    public ResultDto add(LawyerModel lawyerModel) {

        boolean same = lawyerDao.findSameName(lawyerModel);
        if (same) {
            return ResultDto.builder()
                    .code(0)
                    .msg("用户名已存在")
                    .build();
        }

        boolean samePhone = lawyerDao.findSamePhone(lawyerModel);
        if (samePhone) {
            return ResultDto.builder()
                    .code(0)
                    .msg("电话号已存在")
                    .build();
        }

        return ResultDto.builder()
                .code(0)
                .msg("添加成功")
                .count(lawyerDao.add(lawyerModel))
                .build();
    }

    /**
     * 修改数据
     *
     * @param lawyerModel
     * @return
     */
    public ResultDto update(LawyerModel lawyerModel) {
//        boolean same = lawyerDao.findSameName(lawyerModel);
//        if (same) {
//            return ResultDto.builder()
//                    .code(0)
//                    .msg("用户名已存在")
//                    .build();
//        }
//        int count = lawyerDao.Update(lawyerModel);
//        return count;
        return ResultDto.builder()
                .code(0)
                .msg("修改成功")
                .count(lawyerDao.update(lawyerModel))
                .build();
    }

    /**
     * 删除数据
     *
     * @param lawyerModel
     * @return
     */
    public ResultDto delete(LawyerModel lawyerModel) {

//        return lawyerDao.Delete(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("删除成功")
                .count(lawyerDao.delete(lawyerModel))
                .build();
    }


    /**
     * 查询所有律师信息
     *
     * @param lawyerModel
     * @return
     */
    public ResultDto findAll(LawyerModel lawyerModel) {

//        return lawyerDao.FindAll(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .count(lawyerDao.count(lawyerModel))
                .data(lawyerDao.findAll(lawyerModel))
                .build();
    }

    /**
     * 根据id查询
     *
     * @param lawyerModel
     * @return
     */
    public ResultDto findById(LawyerModel lawyerModel) {
        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .data(lawyerDao.findById(lawyerModel))
                .build();
    }

    /**
     * 查询总数
     * @return
     */
    @Override
    public Integer totalCount() {
        return lawyerDao.totalCount();
    }
}
